package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.annotation.DataFilter;
import com.sugon.cache.J2CacheUtils;
import com.sugon.dao.ToolsScrapDao;
import com.sugon.entity.SysMsgEntity;
import com.sugon.entity.ToolsScrapEntity;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.service.ToolsScrapService;
import com.sugon.utils.Constant;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 工装报废Service实现类
 *
 * @author sugon
 * @date 2019-08-05 19:28:35
 */
@Service("toolsScrapService")
public class ToolsScrapServiceImpl implements ToolsScrapService {
    @Autowired
    private ToolsScrapDao toolsScrapDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;

    @Override
    public ToolsScrapEntity queryObject(String id) {
        return toolsScrapDao.queryObject(id);
    }

    @Override
    @DataFilter(userAlias = "CREATE_USER", deptAlias = "FARM_ID")
    public List<ToolsScrapEntity> queryList(Map<String, Object> map) {
        return toolsScrapDao.queryList(map);
    }

    @Override
    @DataFilter(userAlias = "CREATE_USER", deptAlias = "FARM_ID")
    public int queryTotal(Map<String, Object> map) {
        return toolsScrapDao.queryTotal(map);
    }

    /**
     *
     * @param toolsScrap 实体
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(ToolsScrapEntity toolsScrap) {
        toolsScrap.setId(RandomUtil.randomString(32));
        toolsScrap.setDelFlag(1);
        // 设置默认未审核状态
        toolsScrap.setApprovalStatus(0);
        toolsScrap.setPeApprovalStatus(0);
        toolsScrap.setCreateUser(ShiroUtils.getUserId());
        toolsScrap.setCreateDate(new Date());
        String onlyCode = J2CacheUtils.getSequence(4, Constant.TOOL_SCRAP);
        toolsScrap.setOnlyCode(onlyCode);
        int rows = toolsScrapDao.save(toolsScrap);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = toolsScrap.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../tools/toolsscrap.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.TLS_GZBF, msgEntity, 0);
        }
        return rows;
    }

    /**
     *
     * @param toolsScrap 实体
     * @return
     */
    @Override
    public int update(ToolsScrapEntity toolsScrap) {
        toolsScrap.setUpdateDate(DateUtil.date());
        toolsScrap.setUpdateUser(ShiroUtils.getUserId());
        int rows = toolsScrapDao.update(toolsScrap);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = toolsScrap.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../tools/toolsscrap.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.TLS_GZBF, msgEntity, 0);
        }
        return rows;
    }

    /**
     *
     * @param toolsScrap
     * @return
     */
    @Override
    public int check(ToolsScrapEntity toolsScrap) {
        toolsScrap.setQualithApproval(ShiroUtils.getUserId());
        toolsScrap.setApprovalDate(new Date());
        ToolsScrapEntity tsp = toolsScrapDao.queryObject(toolsScrap.getId());
        sysProcessInfoService.saveProcessInfo(toolsScrap.getId(), ProcessInfoEnum.TLS_GZBF, toolsScrap.getApprovalStatus(), null);
        int rows = toolsScrapDao.update(toolsScrap);
        if (rows > 0) {
            int status = toolsScrap.getApprovalStatus();
            if (status == 1) {
                SysMsgEntity msgEntity = new SysMsgEntity();
                String Id = toolsScrap.getId();
                msgEntity.setDataId(Id);
                msgEntity.setIsRead(0);
                msgEntity.setDataUrl("../tools/toolsscrap.html?Id=" + Id);
                sysMsgService.addApproveItem(ProcessInfoEnum.TLS_GZBF, msgEntity, 1);
            }
        }
        return rows;
    }

    /**
     *
     * @param toolsScrap
     * @return
     */
    @Override
    public int peCheck(ToolsScrapEntity toolsScrap) {
        toolsScrap.setPeApproval(ShiroUtils.getUserId());
        toolsScrap.setPeApprovalDate(new Date());
        //执行去掉库存数据
        sysProcessInfoService.saveProcessInfo(toolsScrap.getId(), ProcessInfoEnum.TLS_GZBF, toolsScrap.getPeApprovalStatus(), null);
        int row = toolsScrapDao.update(toolsScrap);
        if (row > 0) {
            SysMsgEntity sysMsgEntity = new SysMsgEntity();
            String Id = toolsScrap.getId();
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataType(ProcessInfoEnum.TLS_GZBF);
            sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
            sysMsgService.allMsgData(sysMsgEntity);
        }
        return row;
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return toolsScrapDao.delete(id);
    }

    /**
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return toolsScrapDao.deleteBatch(ids);
    }
}
